package com.baidu.serviceimpl;

import com.baidu.mapper.PowerMapper;
import com.baidu.pojo.PowerBean;
import com.baidu.pojo.RoleBean;
import com.baidu.service.PowerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 创作时间：2019/10/24 14:05
 * 作者：李增强
 */
@Service
public class PowerServiceImpl implements PowerService {
    @Resource
    PowerMapper powerMapper;

    @Override
    public List<PowerBean> getPowerList() {
        return powerMapper.getPowerList();
    }

    @Override
    public List<RoleBean> getRoleList() {
        return powerMapper.getRoleList();
    }

    @Override
    public List<PowerBean> getPowerListByRid(Integer rid) {
        /**
         * 先把全部权限查出来，需要把当前角色拥有的权限查出来，在后台回显处理
         */
        List<PowerBean> list = powerMapper.getPowerList();
        List<Integer> ids = powerMapper.getRolePowerByRid(rid);
        if(ids!=null&&ids.size()>=1){
            for (Integer id : ids) {
                for (PowerBean pb : list) {
                    if(id.equals(pb.getId())){
                        pb.setChecked(true);
                        break;
                    }
                }
            }
        }
        return list;
    }

    @Override
    public void updateRolePower(Integer rid, String ids) {
        /**
         * 去给角色分配权限之前把原来的先删掉，在增加
         */
        powerMapper.deleteRolePowerByRid(rid);
        if(ids!=null&&ids.length()>=1){
            String[] arr = ids.split(",");
            for (String id:arr){
                powerMapper.saveRolePower(rid,id);
            }
        }
    }
}
